package com.itboye.textileexpres.myset;

import java.io.File;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.tbshopcar.listener.UIAlertView;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.widget.DataCleanManager;

public class ActivitySet extends BaseActivity implements OnClickListener {
	TextView title;
	ImageView back, img_other;
	RelativeLayout account_rel, password_rel, cache_rel;
	Button exit_bt;
	private String str1;
	private long db, sp,cache,dbmanager;
	private TextView tvcler;
	private AlertDialog alertDialog;
	private UIAlertView delDialog;
	String uid;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_set;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		title.setText(R.string.set);
		img_other.setVisibility(View.GONE);
		tvcler = (TextView) findViewById(R.id.tvcler);
		cache_rel = (RelativeLayout) findViewById(R.id.cache_rel);
		cache_rel.setOnClickListener(this);
		onClerDBMager();
		uid = (String) SPUtils.get(ActivitySet.this, null, SpPublicParams.UID,
				"");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.account_rel:
			intent = new Intent(ActivitySet.this, ActivityPersonAccount.class);
			startActivity(intent);
			break;
		case R.id.password_rel:
			intent = new Intent(ActivitySet.this, ActivityPersonPassword.class);
			startActivity(intent);
			break;
		case R.id.cache_rel:
			showDelDialog();
			break;
		case R.id.exit_bt:
			exit();
			finish();
			break;

		default:

			break;
		}
	}
	private void exit() {
		// TODO Auto-generated method stub
		SPUtils.put(MainApplication.context, null, SpPublicParams.USER_NAME, "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.USERHEAD, "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.NICK_NAME, "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.UID, "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.SEX, "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.SIGNATURE, "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.PHONE_MODEL,
				"");
		
		SPUtils.put(MainApplication.context, null, SpPublicParams.URL,
				"");
//		
//		SPUtils.put(MainApplication.context, null, SpPublicParams.IMAGE_URL,
//				"");
		Intent intent=new Intent("newshead");
		sendBroadcast(intent);
	}
	private void showDelDialog() {
		delDialog = new UIAlertView(this, "温馨提示！", "是否清空缓存文件", "否", "是");
		delDialog.show();

		delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {

			public void doLeft() {
				delDialog.dismiss();
			}

			public void doRight() {

				if (!TextUtils.isEmpty(tvcler.getText().toString())
						&& !tvcler.getText().toString().equals(".00B")) {
					new Thread(new MyThread()).start();
				} else {
					cache_rel.setClickable(false);
				}
			}
		});
	}

	public void onClerDBMager() {
	 
		try {
			db = DataCleanManager.getFolderSize(new File("/data/data/"
					+ this.getPackageName() + "/databases"));
			cache=DataCleanManager.getFolderSize(new File("/data/data/"
					+ this.getPackageName() + "/cache"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			sp = DataCleanManager.getFolderSize(new File("/data/data/"
					+ this.getPackageName() + "/shared_prefs"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			long datamanager=db+sp+cache;
			str1 = DataCleanManager.getTotalCacheSize(getApplicationContext(),datamanager);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		DataCleanManager.getFolderSize(cache + eCache + file);

		if (str1 != null && !str1.equals(".00B")) {
			tvcler.setText(str1);
			System.out.println("LLLLLLLLLLLLLLstr" + str1);
			cache_rel.setClickable(true);
		} else {
			tvcler.setText("0.00B");
			System.out.println("LLLLLLLLLLLLLL" + str1);
			cache_rel.setClickable(false);
		}

	}

	/**
	 * 
	 * �������
	 */
	private void clearCache() {
		System.out.println("LLLLLLLLLLLLLclearL" + str1);
		DataCleanManager.cleanInternalCache(this);
		DataCleanManager.cleanExternalCache(this);
		DataCleanManager.cleanFiles(this);
		DataCleanManager.cleanCustomCache(str1);
//		DataCleanManager.cleanApplicationData(getApplicationContext(), str1);
		DataCleanManager.clearAllCache(this);
	}

	class MyThread implements Runnable {

		@Override
		public void run() {
			Message message = new Message();
			message.what = 1;
			handler.sendMessage(message);

		}

	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				clearCache();
				tvcler.setText("0.00B");
				System.out.println("LLLLLLLLLLLLLLewerwerew" + str1);
				MyToast.toast("清空完成！");
				delDialog.dismiss();
				break;
			}

		};
	};

}
